package com.dong.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dong.common.constant.PropertiesConst;
import com.dong.common.util.PropertiesUtils;
import com.dong.common.util.SecureUtils;
import com.dong.module.user.dao.UserMapper;
import com.dong.module.user.pojo.User;
import com.dong.module.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: 陈瑞东
 * @create: 2021-08-25 08:36
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Map<String, String> configParams() {
        Map<String, String> result = new HashMap<>();
        String id = PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_ID);
        String username = PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_USERNAME);
        String password = PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_PASSWORD);
        String autoLogin = PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_AUTOLOGIN);
        String rememberMe = PropertiesUtils.getValue(PropertiesConst.USER_CONFIG_NAME, PropertiesConst.USER_KEY_REMEMBERME);
        result.put("id", id);
        result.put("username", username);
        result.put("password", password);
        result.put("autoLogin", autoLogin);
        result.put("rememberMe", rememberMe);
        return result;
    }

    @Override
    public void login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("username", username);
        // 获取用户
        User user = getOne(queryWrapper);
        // 验证用户
        if (user == null) {
            throw new RuntimeException("该用户不存在！");
        }
        // 加密密码
        String encodePassword = SecureUtils.encodePassword(user.getId(), password);
        if (!user.getPassword().equals(encodePassword)) {
            throw new RuntimeException("密码不正确!");
        }
        // 设置id
        PropertiesUtils.setValue(PropertiesConst.USER_CONFIG_NAME,PropertiesConst.USER_KEY_ID, user.getId());
    }

    @Override
    public void reg(String username, String nickname, String password) {
        // 查询用户名是否已被使用
        User user = getOne(new QueryWrapper<User>().eq("username", username));
        if (user != null) {
            throw new RuntimeException("用户名已被使用！");
        }
        user = new User(username, password, nickname);
        String id = String.valueOf(IdWorker.getId(user));
        user.setId(id);
        // 密码加密
        String encodePassword = SecureUtils.encodePassword(id, password);
        user.setPassword(encodePassword);
        save(user);
    }
}
